AP CSA – Class Notes

1.12 Objects: Instances of Classes

1. Overview

In Java, classes and objects form the foundation of object-oriented programming (OOP). A class is a blueprint that describes how objects of that type behave and what data they store. An object is a real instance created from that blueprint. You can create many different objects from the same class, and each object stores its own unique data.

2. What Is a Class?

A class defines two major parts:

A. Instance Variables (State)

These are the pieces of data every object of the class will store.

private String name; private int age;

B. Methods (Behavior)

These are the actions the object can perform.

public void setAge(int a) { age = a; } public int getAge() { return age; }

A class itself is NOT an object. It is only the design.

3. What Is an Object?

An object is created from a class using the new keyword.

Dog d1 = new Dog(); Dog d2 = new Dog();

Each of these statements creates a new Dog object in memory. The variable (e.g., d1) is a reference variable that points to the object.

4. Objects Hold Different Values

Even though objects come from the same class, they can hold different data.

Student s1 = new Student("Aiden", 95); Student s2 = new Student("Jolie", 87);

s1 stores Aiden's data, while s2 stores Jolie's data. This is why objects represent individual items with their own state.

5. Dot Notation

You access an object's behavior using the dot (.) operator:

s1.getGrade(); s2.setGrade(100);

6. Reference Variables

Object variables do not store the object directly—they store a reference (memory address) pointing to the object.

Student s1 = new Student("Ben", 90);

7. Real-World Analogy

Class: Car Blueprint
Objects: Actual Cars (Toyota, Honda, Tesla)

Each car shares the same design but has different colors, mileage, and owners. This is exactly how class instances differ in Java.

8. Key Terms

TermDefinition
ClassBlueprint defining attributes (state) and methods (behavior)
ObjectA real instance created from a class
Instance VariableData stored inside each object
MethodBehavior an object can perform
InstantiationCreating an object using new
StateThe values stored in an object
BehaviorActions the object performs
Reference VariableStores the memory address of an object

9. Example Class & Objects

Class Definition

public class Dog { private String name; private int age; public Dog(String n, int a) { name = n; age = a; } public void bark() { System.out.println(name + " says Woof!"); } }

Creating Objects

Dog d1 = new Dog("Max", 4); Dog d2 = new Dog("Bella", 2);

Using Objects

d1.bark(); // Max says Woof! d2.bark(); // Bella says Woof!

10. Importance in AP CSA

Understanding objects helps students succeed in:

Objects are the building blocks of Java programs.

Coding Exercises

Exercise 1 — Class vs. Object Identification

Given a class Book and a few reference declarations, determine:

Exercise 2 — Create Two Objects with Different Values

Write a Player class with name and score, then create two different players in the client to show how they store different values.

Exercise 3 — Object Tracing

Trace what happens to the values of two Counter objects when methods are called and references are reassigned.

Exercise 4 — Write a Class + Use Multiple Objects

Create a Rectangle class with area(), perimeter(), and isSquare(). Make three Rectangle objects and print their results.

Exercise 5 — Mini-FRQ: BankAccount

Write a BankAccount class with deposit/withdraw. Create two accounts in the client and show how their balances change independently.

Exercise 6 — Objects in an Array

Create an array of 4 Player objects, initialize them in a loop, and print each player's score.

Exercise 7 — ArrayList of Objects

Create a Pet class. Store several Pet objects in an ArrayList, then loop and call speak() on each.

Exercise 8 — Add a toString()

Add a meaningful toString() method to any class you wrote and print the object.

Exercise 9 — Reference Aliasing

Practice scenarios where two references point to the same object, then one reference changes.

Exercise 10 — Design Your Own Class

Create a class of your choice with at least 3 instance variables and 3 methods. Then create two distinct objects to show different stored values.